import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
import TableControls from '#cpt/temp/TableControls'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const FillParameterModal	= $Lazy.state(()=>import('./cpt/fill-parameter-modal'))
const LookModal	= $Lazy.state(()=>import('./cpt/look-modal'))
// ===================================================================== Data
const statusData = [{ name:'正常', value: '1' }, { name:'作废中', value:'2' }, { name:'已作废', value:'3' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',			name: 'summary_sn', 	type:'select', loading:true },
			{ label: '受检单位',		name: 'hosp_id', 		type:'select', loading:true },
			{ label: '业务员',		name: 'salesman_id', 	type:'select', loading:true },
			{ label: '发票状态',		name: 'status', 		type:'select', data:statusData },
			{ label: '开票单位',		name: 'organization_name' },
			{ label: '操作时间',		names: ['start_at','end_at'], name:'time-null', type:'date-range' },
		],
		formData:[
			{ label: '账期',			name: 'summary_sn', type: 'select', 	full:true,	loading:true, required:true, 
				onValue:({value,formData})=>{
					const {hosp_id} = this.formRef.getValue(['hosp_id'])
					if(value&&hosp_id)this.getBill({hosp_id,summary_sn:value})
				} ,
			},
			{ label: '受检单位',		name: 'hosp_id', 	type: 'select', 	half:true,	loading:true, nameStr:'name', required:true,
				onValue:({value,formData})=>{
					const {summary_sn} = this.formRef.getValue(['summary_sn'])
					if(value&&summary_sn)this.getBill({summary_sn,hosp_id:value})
				}
			 },
			{ label: '开票单位',		name: 'organization_name', 	half:true, required:true },
			{ label: '暂估金额',		name: 'estimate_price', 	half:true, required:true },
			{ label: '业务员',		name: 'salesman_id', 		type: 'select', 	half:true, 
				required:true, nameStr:'real_name', idStr:'salesman_id'
			},
		]
	}
	
	model = {}
	power = {}
	
	componentDidMount(){
		const { hosp_id } = $fn.query()
		$http.submit(this,'fi-standing-book/checkPermission').then(data=>{
			this.power = data
		})
		if(hosp_id){ 
			this.query = { hosp_id: hosp_id}
			this.hosp_id = hosp_id
		}
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 设置医院名称
			this.searchFormRef.setValue({ hosp_id: this.hosp_id })
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'fi-invoice-book/index', { isIndex:true })
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 		width:100 },
		{ title: '单位编码', 	dataIndex: 'hosp_code', 		width:100 },
		{ title: '受检单位', 	dataIndex: 'hosp_name', 		width:200 },
		{ title: '业务员编码', 	dataIndex: 'salesman_code', 	width:100 },
		{ title: '业务员', 		dataIndex: 'salesman_user', 	...$fn.table.name },
		{ title: '开票单位', 	dataIndex: 'organization_name', width:200 },
		{ title: '账单金额', 	dataIndex: 'bill_price', 		...$fn.table.num },
		{ title: '发票类型', 	dataIndex: 'invoice_type_name', width:100 },
		{ title: '发票状态', 	dataIndex: 'status_name', 		width:100 },
		{ title: '开票金额', 	dataIndex: 'invoice_price', 	...$fn.table.num },
		{ title: '不开票金额', 	dataIndex: 'not_invoice_price', ...$fn.table.num },
		{ title: '未开票金额', 	dataIndex: 'wait_invoice_price',...$fn.table.num },
		{ title: '申请人', 		dataIndex: 'apply_user_name', 	...$fn.table.name },
		{ title: '申请时间', 	dataIndex: 'apply_at', 			...$fn.table.time },
		{ title: '审核人', 		dataIndex: 'audit_user_name', 	...$fn.table.name },
		{ title: '审核时间', 	dataIndex: 'audit_at', 			...$fn.table.time },
		{ title: '操作人', 		dataIndex: 'add_user_name', 	...$fn.table.name },
		{ title: '操作时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '备注', 		dataIndex: 'remark', 			...$fn.table.num },
		TableControls.controls.call(this, { width: 180 })
	]
	
	// 表格操作按钮
	table_controls = (text,{uuid,status,summary_sn,hosp_id}) => {
		return [
			+status === 1 && Confirm.call(this, '申请作废', 'fi-invoice-book/invalid', { query:{uuid:[uuid],status:2} }),
			+status === 2 && Confirm.call(this, '发票作废', 'fi-invoice-book/comfirmInvalid', { query:{uuid:[uuid],status:3} }),
			TableControls.click.call(this, '查看', ()=> this.lookModal.open({uuid,summary_sn,hosp_id}) ),
			+status !== 3 && TableControls.click.call(this, '编辑', ()=> this.fillParameterModal.open({uuid,summary_sn,hosp_id}) ),
		]
	}
	
	// 操作按钮
	controls = () => {
		const { selectedRows } = this.state
		const uuid = selectedRows.map((v,i)=>v.uuid)
		const { fiinvoicebook_save, fiinvoicebook_invalid, fiinvoicebook_import, fiinvoicebook_comfirminvalid } = this.power
		return [
			// 填写发票申请
			+fiinvoicebook_save === 1 ? Controls.click.call(this, '填写发票申请', () => this.fillParameterModal.open(selectedRows)) : null,	
			// 申请作废
			+fiinvoicebook_invalid === 1 ? Confirm.call(this, '申请作废', 'fi-invoice-book/invalid', { query:{uuid,status:2}, isKeys:true }) : null,
			// 确认作废
			+fiinvoicebook_comfirminvalid === 1 ? Confirm.call(this, '确认发票作废', 'fi-invoice-book/comfirmInvalid', { query:{uuid,status:3}, isKeys:true }) : null,
			// 导入
			+fiinvoicebook_import === 1 ? Controls.imports.call(this, 'fi-invoice-book/import') : null,
			this.hosp_id && Controls.back.call(this)	// 返回
		]
	}
	
	render(){
		return (
			<>
				<Page title='已开发票' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, {
						onForm:()=>{
							this.getSelect()
						}
					}) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit, isIndex:true}) }
				</Page>
				{/* 填写发票申请 */}
				<FillParameterModal onRef={ref=>this.fillParameterModal = ref} onRefresh={this.fetch}/>
				{/* 查看 */}
				<LookModal onRef={ref=>this.lookModal=ref} />
			</>
		)
	}
}

export default Index